package com.lagou.eduorderboot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.eduorderboot.entity.UserCourseOrder;
import com.lagou.eduorderboot.mapper.OrderDao;
import com.lagou.eduorderboot.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: lagou-edu
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-09-09 16:00
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    public void saveOrder(String orderNo, String user_id, String course_id, String activity_course_id, String source_type) {
        //orderDao.saveOrder(orderNo, user_id, course_id, activity_course_id, source_type);
        UserCourseOrder order = new UserCourseOrder();
        order.setOrderNo(orderNo);
        order.setUserId(user_id);
        order.setCourseId(course_id);
        order.setActivityCourseId(Integer.parseInt(activity_course_id));
        order.setSourceType(source_type);
        order.setIsDel(1);
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        orderDao.insert(order);
    }

    public Integer updateOrder(String orderNo, int status) {
        //return orderDao.updateOrder(orderNo,status);
        UserCourseOrder userCourseOrder = new UserCourseOrder();
        userCourseOrder.setStatus(status);

        //创建查询构造器
        QueryWrapper<UserCourseOrder> queryWrapper = new QueryWrapper<>();
        //添加查询条件
        queryWrapper.eq("order_no",orderNo);

        return orderDao.update(userCourseOrder,queryWrapper);
    }

    public Integer deleteOrder(String orderNo) {
        //return orderDao.deleteOrder(orderNo);
        //创建查询构造器
        QueryWrapper<UserCourseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        return orderDao.delete(queryWrapper);
    }

    public List<UserCourseOrder> getOKOrderCourseIds(String userId) {
        //return orderDao.getOrdersByUserId(userId);
        //select course_id from edu_order.user_course_order where status = 20 and is_del = 0  and  user_id = #{userId}
        //创建查询构造器
        QueryWrapper<UserCourseOrder> queryWrapper = new QueryWrapper<>();
        //添加查询条件
        queryWrapper.select("course_id"); //相当于：select course_id from edu_order.user_course_order
        queryWrapper.eq("STATUS",20);//相当于：where status = 20
        queryWrapper.eq("is_del",0);//相当于：and is_del = 0
        queryWrapper.eq("user_id",userId);//相当于：and  user_id = #{userId}
        return orderDao.selectList(queryWrapper);
    }

}
